# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/9/16 11:13'

'''
采用单例模式实现日志功能
'''
import  logging
from logging import handlers
#from k3_data_report.config import LOG_FILE_DIR
LOG_FILE_DIR = r'E:\obs-studio'
#类的单例模式,装饰器
class Singlecls(object):
    def __init__(self, cls):
        self._cls = cls
        self._instance = {}
    def __call__(self):
        if self._cls not in self._instance:
            self._instance[self._cls] = self._cls()
        return self._instance[self._cls]


@Singlecls
class wLoggerCls(object):
    def __init__(self, init_logfile='data_report.log', level=logging.INFO):
        self.logger = logging.getLogger(__name__)
        self.init_log(init_logfile, level)

    def init_log(self, init_logfile, level):
        '''
        初始化
        :param init_logfile:  初始化日志文件
        :param level: 日志级别，水位线 logging.DEBUG，logging.INFO...
        :return:
        '''
        # filename定义将信息输入到指定的文件，when指定单位是s(秒),interval是时间间隔的频率,
        # 单位是when所指定的（你可以理解频率是5s）；
        # backupCount表示备份的文件个数，我这里是指定的3个文件。
        log_file = LOG_FILE_DIR + "\\" + init_logfile
        fh = handlers.TimedRotatingFileHandler(filename=log_file, when="H", interval=1, backupCount=7)
        # 定义输出格式
        formatter = logging.Formatter('[%(levelname)s][%(asctime)s]-[%(message)s]')
        fh.setFormatter(formatter)  # 添加格式化输出
        logging.root.setLevel(level=level)
        self.logger.addHandler(fh)


    def winfo(self, message):
        self.logger.info(message)


    def wdebug(self,message):
        self.logger.debug(message)


    def werror(self, message):
        self.logger.error(message)


    def wcritical(self, message):
        self.logger.critical(message)


    def warning(self, message):
        self.logger.warning(message)



g_wlogger = wLoggerCls()